import com.entity.*;
import com.mapper.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017/6/21.
 */
public class Dom {
    protected SqlSessionFactory sf;
    protected SqlSession ss;
    protected BookMapper bdao;
    protected UserMapper user;

    protected MemberMapper mem;

    protected DbuserMapper dbuser;
    protected DbroleMapper dbrole;
    @Before
    public void init(){

        try {
            this.sf=new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
            this.ss=this.sf.openSession();
            bdao= this.ss.getMapper(BookMapper.class);
            user=this.ss.getMapper(UserMapper.class);

            mem= this.ss.getMapper(MemberMapper.class);

            dbuser= this.ss.getMapper(DbuserMapper.class);
            dbrole =this.ss.getMapper(DbroleMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @After
    public void  close(){
        this.ss.commit();
        this.ss.close();
    }
    @Test
    public void  show() {

       /* Map<String,Object> map= new HashMap<String,Object>();
        map.put("name","强强");
        List<Book> list = bdao.query(map);
        for(Book book: list){

            System.out.println(book.getName());
        }*/

  /*  User user1= user.queryByAccount("admin");
        System.out.println(user1);
        System.out.println(user1.getAccount());
        System.out.println(user1.getUserinfo().getAddress());*/

        /* Member m= mem.queryById(2);
        System.out.println(m);
        System.out.println(m.getName());
        System.out.println(m.getAddress());
        System.out.println(m.getAddress().getAphone());*/

        /*List<Map<String,Object>> list = mem.query();
        for(Map<String,Object> mm : list){
            System.out.println(mm.get("name")+ "="+ mm.get("aname"));
            System.out.println(mm);
        }*/

       /* List<Map<String, Object>> list = mem.queryAll();
        for (Map<String, Object> m : list) {
            System.out.println(m.get("name") + "=" + m.get("aname"));
            System.out.println(m);

        }*/


      /* ***********一对多**********/

      Dbuser dbu =dbuser.findById(1);
        System.out.println(dbu.getPass());
       // System.out.println(dbu.getDbroles());
        List<Dbrole> uu = dbu.getDbroles();
        for(Dbrole u:uu){
            System.out.println(u.getName());
        }


        List<Map<String,Object>> list  = dbuser.findAll();
        for(Map<String,Object> mm:list){
            System.out.println(mm);
            System.out.println(mm.get("name"));
            System.out.println(mm.get("name"));
            System.out.println(mm.get("uid"));


        }


      /* Dbrole dbro = dbrole.findById(1);
        System.out.println(dbro);
        System.out.println(dbro.getName());*/

    }


}
